<?php
namespace app\admin\controller\order;
use think\Db;

class Order extends \app\admin\controller\Common
{
    public function index(){
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
    
        $query = (new \app\common\model\Orders())
                ->field([
                    'id','type','order_on','total_money','income_money','money','state','store_id','rider_id','pay_time'
                ]);
 
        isset($search['title']) && $search['title'] != "" ? $query->where("title like '%{$search['content']}%'") : '';
        $query->order("id desc");
        $list = $query->limit($page, $limit)->append(['type_text','state_text','store_name','pay_time_text','rider_name'])->select();
        $count = $query->count();
        return self::successResult('',$list,$count);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = request()->param();
      
            // $validate = new \app\common\validate\Menu();
            // $result = $validate->check($data);
            // if ($result === false) {
            //     $this->error($validate->getError());
            // }
            Db::startTrans();
            try {
                $message = "订单创建失败,请重试";
                #创建商家
                $store_id = Db::table("{$this->tdb}store")->insertGetId($data['info']);
                if(!$store_id){
                    $message = "商家创建失败,请重试";
                    $process = false;
                }
                #创建证明资料
                $data['full']['store_id'] = $store_id;
                $row = Db::table("{$this->tdb}store_information")->insert($data['full']);
                if(!$row){
                    $message = "证明资料创建失败,请重试";
                    $process = false;
                }
                $process = true;
                if ($process !== false) {
                    Db::commit(); // 提交事务
                } else {
                    Db::rollback(); // 回滚事务
                }
            }catch (\Exception $e) {
                $process = false;
                $message = $e->getMessage();
                Db::rollback(); // 回滚事务
            }
            var_dump($message);die;
            // $table->allowField(true)->save($data['info']);
            // if ($table->id) {
            //     return self::successResult();
            // } else {
            //     return self::errorResult();
            // }
        } else {
            $category = (new \app\common\model\StoreCategory())->order('sort desc')->select();
            $this->assign('category',$category);
            return view();
        }
    }
    
    public function info()
    {
        if (request()->isPost()) {
            $data = request()->param();
            $validate = new \app\common\validate\Menu();
            $result = $validate->check($data);
            if ($result === false) {
                $this->error($validate->getError());
            }
            $table = new \app\common\model\Menu();
            $row = $table->allowField(true)->save($data, ['id' => $data['id']]);
        
            if ($row) {
                return self::successResult();
            } else {
                return self::errorResult();
            }
        } else {
            $id = intval(request()->param('id'));
            $info = (new \app\common\model\Orders())->where(['id' => $id])->append([
                'type_text','state_text','store_name','pay_time_text','rider_name',
                'store_address','agent','weather_text','store_is_state','store_state_text','estimate_meal_time_text',
                'user_text','rider_receiving_time_text','rider_success_time_text'
                ])->find();
            $sku = (new \app\common\model\OrdersGoods())->where(['order_id' => $id])->select();
            $this->assign('info',$info);
            $this->assign('sku',$sku);
            return view();
        }
    }

    public function delete(){
        $id = request()->param('id');
        if (empty($id)) $this->error("请选择要删除的数据");
        $table = new \app\common\model\Store();
        if (is_array($id)) {
            $id = implode(",", $id);
            $row = $table->whereIn('id', $id)->delete();
        } else {
            $row = $table->where('id', $id)->delete();
        }
         return $row ? self::successResult("成功") : self::errorResult("失败");
    }

    public function status(){
        $id = request()->param('id');
        $info = (new \app\common\model\StoreInformation())->alias('info')
                ->join("store store","store.id = info.store_id")
                ->field([
                    'info.id','store.title','info.full_name','info.store_id',
                    'info.full_id','info.full_id_image','info.storefront_image',
                    'info.registration','info.permit_image','info.restaurant'
                    ])
                ->where(['store_id' => $id])->find();
        if(empty($info)){
            return  "没有上传资料";
        }
        $this->assign('info',$info);
        return view();
    }
    public function certification(){
        $data = request()->param();
        if ($data['audit'] == 'yes') {
            (new \app\common\model\Store())->where('id', $data['id'])
                ->update(['status' => 2, 'status_fail' => '']);
        } else {
            (new \app\common\model\Store())->where('id', $data['id'])
                ->update(['status' => 3, 'status_fail' => $data['status_fail']]);
        }
        return self::successResult();
    }

  

 
}
